home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_300 / 310_02 / parser.lex < prev    next >
Text File  |  1990-04-18  |  4KB  |  141 lines

  1. %{
  2. /*
  3.         Little Smalltalk lexical analyzer
  4. */
  5. # include <math.h>
  6. # include "primnum.h"
  7.  
  8. # undef input
  9. # undef unput
  10.  
  11. double atof();
  12. int linenum = 1;
  13. %}
  14. %%
  15. [ \t]+                          {;}
  16. \n                              {linenum++;}
  17. \"                              {readcomment();}
  18. ":="                            {return(ASSIGN);}
  19. "<-"                            {return(ASSIGN);}
  20. Class                           {return(lexsave(CLASS));}
  21. self                            {yylval.p = selfvar;  return(PSEUDO);}
  22. selfProcess            {yylval.p = procvar;  return(PSEUDO);}
  23. super                           {yylval.p = supervar; return(PSEUDO);}
  24. nil                             {yylval.p = nilvar;   return(PSEUDO);}
  25. true                            {yylval.p = truevar;  return(PSEUDO);}
  26. false                           {yylval.p = falsevar; return(PSEUDO);}
  27. smalltalk                       {yylval.p = smallvar; return(PSEUDO);}
  28. \$.                             {yylval.i = yytext[1]; return(LITCHAR);}
  29. #                               {return(PS);}
  30. [0-9]+r-?[0-9A-Z]+(\.[0-9A-Z]+)?(e[-+]?[0-9]+)? {return(lexsave(LITFNUM));}
  31. [0-9]+                          {yylval.i = atoi(yytext); return(LITNUM);}
  32. [0-9]+(\.[0-9]+)?(e[-+]?[0-9]+)?   {return(lexsave(LITFNUM));}
  33. '[^']*'                         {char c; unput(c = input());
  34.                                  if (c == '\'') yymore();
  35.                                  else return(lexlstr());}
  36. [a-zA-Z0-9]+:?                  {return(varlex());}
  37. :[a-zA-Z0-9]+                   {return(slexsave(COLONVAR));}
  38. #[^ \t\n.()\[]+                 {return(slexsave(LITSYM));}
  39. "-"                             {return(lexsave(MINUS));}
  40. "("                             {return(LP);}
  41. ")"                             {return(RP);}
  42. "["                             {return(LB);}
  43. "]"                             {return(RB);}
  44. "."                             {return(PERIOD);}
  45. ^"|"                {return(lexsave(MBAR));}
  46. ^"!"                {return(lexsave(MBAR));}
  47. "|"                             {return(lexsave(BAR));}
  48. "!"                             {return(lexsave(BAR));}
  49. ";"                             {return(SEMI);}
  50. "^"                             {return(lexsave(UPARROW));}
  51. ">"                {return(lexsave(PE));}
  52. [^ \t\nA-Za-z0-9]               {return(lexsave(BINARY));}
  53. "<primitive"               {return(PRIMITIVE);}
  54. "<"[a-zA-Z0-9]+            {yylval.i = prim_number(&yytext[1]); return(NAMEDPRIM);}
  55. %%
  56. static int ocbuf = 0;
  57. static int pbbuf[400];
  58.  
  59. static int input()
  60. {    int c;
  61.  
  62.     if (ocbuf) {c = pbbuf[--ocbuf]; }
  63.     else {
  64.         c = getc(fp);
  65.         if (c == EOF) c = 0;
  66.         }
  67.     return(c);
  68. }
  69.  
  70. static unput(c)
  71. char c;
  72. {
  73.     if (c) pbbuf[ocbuf++] = c;
  74. }
  75.  
  76. # include <ctype.h>
  77.  
  78. static readcomment()
  79. {  char c;
  80.  
  81.    while ((c = input()) && c != '\"')
  82.     if (c == '\n') linenum++;
  83.    if (!c) yyerror("unterminated comment");
  84. }
  85.  
  86. char *walloc(s) char *s;
  87. {  char *p, *malloc();
  88.  
  89.    p = malloc((unsigned) (strlen(s) + 1));
  90.    if (p == (char *) 0) yyerror("out of variable string space");
  91.    strcpy(p, s);
  92.    return(p);
  93. }
  94.  
  95. static int slexsave(type)
  96. int type;
  97. {
  98.  
  99.     yylval.c = walloc(&yytext[1]);
  100.     if (yylval.c == 0) yerr("cannot create symbol %s", yytext);
  101.     return(type);
  102. }
  103.  
  104. static int lexsave(type)
  105. int type;
  106. {
  107.  
  108.     yylval.c = walloc(yytext);
  109.     if (yylval.c == 0) yerr("cannot create string %s", yytext);
  110.     return(type);
  111. }
  112.  
  113. static int varlex()
  114. {  
  115.  
  116.    lexsave(0);
  117.    if (yytext[yyleng-1] == ':') return(KEYWORD);
  118.    else if (islower(yytext[0])) return(LOWERCASEVAR);
  119.    else return(UPPERCASEVAR);
  120. }
  121.  
  122. static int lexlstr()
  123. {  char *p, *q;
  124.  
  125.    yylval.c = p = walloc(&yytext[1]);
  126.    *(p + yyleng -2) = '\0';
  127.    return(LITSTR);
  128. }
  129.  
  130. static int prim_number(name)
  131. char *name;
  132. {    struct prim_names *p;
  133.  
  134.     for (p = prim_table; *(p->p_name); p++) {
  135.         if (strcmp(p->p_name, name) == 0)
  136.             return(p->p_number);
  137.         }
  138.     yerr("unknown primitive name %s", name);
  139.     return(0);
  140. }
  141.